package cn.jjxx.modules.map.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jjxx.core.common.service.impl.CommonServiceImpl;
import cn.jjxx.core.utils.MapBeanUtil;
import cn.jjxx.core.utils.MessageUtils;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.map.entity.ColumnsInfo;
import cn.jjxx.modules.map.entity.RelateInfo;
import cn.jjxx.modules.map.entity.TablesInfo;
import cn.jjxx.modules.map.mapper.CommonMapMapper;
import cn.jjxx.modules.map.service.ICommonMapService;


@Transactional
@Service("commonMapService")
public class CommonMapServiceImpl extends CommonServiceImpl<CommonMapMapper,T> implements ICommonMapService{

	@Override
	public List<Map<String, Object>> selectDbTables(String dbName,
			String tableName, String tableComment) {
		return baseMapper.selectDbTables(dbName, tableName, tableComment);
	}

	@Override
	public List<Map<String, Object>> selectTableInfos(String dbName,
			String tableName) {
		return baseMapper.selectTableInfos(dbName, tableName);
	}

	@Override
	public List<Map<String, Object>> selectBySql(String sql) {
		return baseMapper.selectBySql(sql);
	}

	@Override
	public List<Map<String, Object>> selectRelateInfo(String dbName,
			String tableName) {
		return baseMapper.selectRelateInfo(dbName, tableName);
	}

	@Override
	public Long checkQuoteInfos(List<RelateInfo> relateInfos, List<String> ids) {
		return baseMapper.checkQuoteInfos(relateInfos, ids);
	}

	@Override
	public Long checkQuoteInfo(RelateInfo relateInfo, List<String> ids) {
		return baseMapper.checkQuoteInfo(relateInfo, ids);
	}

	@Override
	public List<TablesInfo> selectTableList(String dbName, String tableName,
			String tableComment) {
		List<TablesInfo> beanList = new ArrayList<TablesInfo>();
		List<Map<String, Object>> list = baseMapper.selectDbTables(dbName, tableName, tableComment);
		for(Map<String, Object> map:list){
			TablesInfo table = new TablesInfo();
			try {
				table = (TablesInfo) MapBeanUtil.convertMap(TablesInfo.class, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
			beanList.add(table);
		}
		return beanList;
	}

	@Override
	public List<ColumnsInfo> selectTableInfoList(String dbName,String tableName) {
		List<ColumnsInfo> beanList = new ArrayList<ColumnsInfo>();
		List<Map<String, Object>> list = baseMapper.selectTableInfos(dbName, tableName);
		for(Map<String, Object> map:list){
			ColumnsInfo tableInfo = new ColumnsInfo();
			try {
				tableInfo = (ColumnsInfo) MapBeanUtil.convertMap(ColumnsInfo.class, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
			beanList.add(tableInfo);
		}
		return beanList;
	}

	@Override
	public List<RelateInfo> selectRelateList(String dbName, String tableName) {
		List<RelateInfo> beanList = new ArrayList<RelateInfo>();
		List<Map<String, Object>> list = baseMapper.selectRelateInfo(dbName, tableName);
		for(Map<String, Object> map:list){
			RelateInfo relateInfo = new RelateInfo();
			try {
				relateInfo = (RelateInfo) MapBeanUtil.convertMap(RelateInfo.class, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
			beanList.add(relateInfo);
		}
		return beanList;
	}

	@Override
	public String quoteMsg(String dbName, String tableName,List<String> bills) {
		if(!StringUtils.isNotEmpty(dbName)){
			dbName = MessageUtils.getMessage("connection.dbname");
		}
		List<RelateInfo> riList = this.selectRelateList(dbName, tableName);
		for(RelateInfo relateInfo:riList){
			Long num = this.checkQuoteInfo(relateInfo, bills);
			if(num>0){
				List<TablesInfo> tables = this.selectTableList(dbName, relateInfo.getTableName(), null);
				return num+"条数据在【"+tables.get(0).getTableComment()+"】引用";
			}
		}
		return null;
	}

	@Override
	public boolean updateBySql(String sql) {
		return baseMapper.updateBySql(sql);
	}

	@Override
	public boolean insertBySql(String sql) {
		return  baseMapper.insertBySql(sql);
	}

	@Override
	public boolean deleteBySql(String sql) {
		return  baseMapper.deleteBySql(sql);
	}
	
}
